package com.hancher.gamelife2.birthday;

import com.google.gson.Gson;
import com.hancher.common.androidutil.LogUtil;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/5/31 12:41 <br/>
 * 描述 :
 */
public class BirthdayUtil {
    /**
     * 一条生日记录，转换为两条生日记录（阴历、阳历）
     * @param list 原始生日
     * @return 阴阳历生日
     */
    public static List<BirthdayPO> birthday2LunarSolarBirthday(List<BirthdayPO> list, boolean isShowAll) {
        List<BirthdayPO> lunarAndSolarBirthdayList = new ArrayList<>();
        for (BirthdayPO item : list) {
            Gson gson = new Gson();
            String gsonStr = gson.toJson(item);

            //阴历
            BirthdayPO lunarBirthday = gson.fromJson(gsonStr, BirthdayPO.class);
            lunarBirthday.setIsLunarType(true);
            //倒计时
            Lunar current = Lunar.fromDate(new Date());
            Lunar currentYearBirthday = null;
            try {
                currentYearBirthday = new Lunar(current.getYear(), Math.abs(lunarBirthday.getLunarM()), lunarBirthday.getLunarD());
            } catch (Exception e) {
                LogUtil.d("农历获取异常:" + e);
            }
            if (currentYearBirthday == null || currentYearBirthday.getSolar().subtract(current.getSolar()) < 0) {
                try {
                    currentYearBirthday = new Lunar(current.getYear() + 1, Math.abs(lunarBirthday.getLunarM()), lunarBirthday.getLunarD());
                } catch (Exception e) {
                    LogUtil.d("农历下一年获取异常：" + e);
                }
            }
            if (currentYearBirthday != null) {
                lunarBirthday.setCountdownAge(current.getYear() - lunarBirthday.getLunarY());
                lunarBirthday.setCountdownDay(currentYearBirthday.getSolar().subtract(current.getSolar()));
                if (lunarBirthday.getIsLunar() || isShowAll) {
                    lunarAndSolarBirthdayList.add(lunarBirthday);
                }
            }
//            if (lunarBirthday.getCoutdownDay() == 0) {
//                lunarAndSolarBirthdayList.add(lunarBirthday);
//            }

            //阳历
            BirthdayPO solarBirthday = gson.fromJson(gsonStr, BirthdayPO.class);
            solarBirthday.setIsLunarType(false);
            //倒计时
            Solar current2 = Solar.fromDate(new Date());
            Solar currentYearBirthday2 = new Solar(current2.getYear(), solarBirthday.getSolarM(), solarBirthday.getSolarD());
            if (currentYearBirthday2.subtract(current2) < 0) {
                currentYearBirthday2 = new Solar(current2.getYear() + 1, solarBirthday.getSolarM(), solarBirthday.getSolarD());
            }
            solarBirthday.setCountdownDay(currentYearBirthday2.subtract(current2));
            solarBirthday.setCountdownAge(current2.getYear() - solarBirthday.getSolarY());
            if (lunarBirthday.getIsSolar() || isShowAll) {
                lunarAndSolarBirthdayList.add(solarBirthday);
            }
//            if (solarBirthday.getCoutdownDay() == 0) {
//                lunarAndSolarBirthdayList.add(solarBirthday);
//            }
        }
        // 排序
        lunarAndSolarBirthdayList.sort((o1, o2) -> o1.getCountdownDay().intValue() - o2.getCountdownDay().intValue());
        LogUtil.d("从数据库读取的数据，转化为阴阳历后的个数：" + lunarAndSolarBirthdayList.size());
        return lunarAndSolarBirthdayList;
    }
}
